<template>
	<view class="blacklist-container">
		<scroll-view scroll-y class="list-scroll" @scrolltolower="loadMore" :refresher-enabled="true"
			:refresher-triggered="refreshing" @refresherrefresh="onRefresh">
			<!-- 黑名单列表 -->
			<view class="blacklist">
				<view class="user-item" v-for="user in blackList" :key="user.id">
					<view class="user-info" @tap="goToProfile(user.sysUser.userId)">
						<image class="avatar" :src="user.sysUser.avatar || '/static/avatars/default-avatar.jpg'"
							mode="aspectFill"></image>
						<view class="info">
							<text class="nickname">{{user.sysUser.nickName}}</text>
							<text class="block-time">拉黑时间：{{user.createTime}}</text>
						</view>
					</view>
					<button class="unblock-btn" @tap="handleUnblock(user)">移除黑名单</button>
				</view>
			</view>

			<!-- 空状态 -->
			<view class="empty-state" v-if="blackList.length === 0">
				<image src="/static/images/empty-data.png" mode="aspectFit"></image>
				<text>暂无黑名单</text>
			</view>

			<!-- 加载更多 -->
			<view class="loading-more" v-if="blackList.length > 0">
				<text v-if="hasMore">加载中...</text>
				<text v-else>没有更多了</text>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	import {
		getMyBlackList,
		updateFriend
	} from '@/api/youqiu/friend.js'

	export default {
		data() {
			return {
				blackList: [],
				refreshing: false,
				hasMore: false
			}
		},

		onLoad() {
			this.getBlackList()
		},

		methods: {
			// 获取黑名单列表
			async getBlackList() {
				try {
					const res = await getMyBlackList()
					if (res.code === 200) {
						this.blackList = res.data || []
					}
				} catch (error) {
					console.error('获取黑名单失败:', error)
					uni.showToast({
						title: '获取黑名单失败',
						icon: 'none'
					})
				}
			},

			// 移除黑名单
			async handleUnblock(user) {
				try {
					const res = await updateFriend({
						friendId: user.friendId,
						isBlocked: 0,
						userId: user.userId
					})

					if (res.code === 200) {
						uni.showToast({
							title: '已移除黑名单',
							icon: 'success'
						})
						// 刷新列表
						this.getBlackList()
					}
				} catch (error) {
					uni.showToast({
						title: error.message || '操作失败',
						icon: 'none'
					})
				}
			},

			// 跳转到用户主页
			goToProfile(userId) {
				uni.navigateTo({
					url: `/pages/profile/detail?userId=${userId}`
				})
			},

			// 下拉刷新
			async onRefresh() {
				this.refreshing = true
				await this.getBlackList()
				this.refreshing = false
			}
		}
	}
</script>

<style lang="scss" scoped>
	.blacklist-container {
		min-height: 100vh;
		background: #f5f5f5;

		.list-scroll {
			height: 100vh;
			padding: 20rpx;
			box-sizing: border-box;
		}

		.user-item {
			background: #fff;
			border-radius: 12rpx;
			padding: 30rpx;
			margin-bottom: 20rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.user-info {
				display: flex;
				align-items: center;
				flex: 1;
				margin-right: 20rpx;

				.avatar {
					width: 100rpx;
					height: 100rpx;
					border-radius: 50%;
					margin-right: 20rpx;
				}

				.info {
					.nickname {
						font-size: 32rpx;
						color: #333;
						margin-bottom: 8rpx;
						display: block;
					}

					.block-time {
						font-size: 24rpx;
						color: #999;
					}
				}
			}

			.unblock-btn {
				min-width: 160rpx;
				height: 60rpx;
				line-height: 60rpx;
				background: #ff6b35;
				color: #fff;
				font-size: 26rpx;
				border-radius: 30rpx;
				padding: 0 30rpx;
				margin: 0;

				&:active {
					opacity: 0.8;
				}
			}
		}

		.empty-state {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 100rpx 0;

			image {
				width: 200rpx;
				height: 200rpx;
				margin-bottom: 20rpx;
			}

			text {
				font-size: 28rpx;
				color: #999;
			}
		}

		.loading-more {
			text-align: center;
			padding: 20rpx 0;
			font-size: 24rpx;
			color: #999;
		}
	}
</style>